<?php


namespace app\api\controller;
use app\api\model\Base as bs;
use think\Db;
use think\Request;

class Classroom extends Base
{
    public function classroom(){
        $head = $this->request = Request::instance()->header();
        if(substr($head['token'], -1)==4){
            $this->apiReturn(101,'敢越过权限，你胆子蛮大嘛');
        }
        if(Request::instance()->method()== 'GET'){
            $page = input('current','1','intval');
            $page_size = input('pageSize',10,'intval');
            $like = I("id|name|remark");
            $data = Db::name('classroom')->field('id,name,remark')->field(['id'=>'key'])->where('status',1)
                ->where('id', 'like', '%'.$like['id'].'%')
                ->where('name', 'like', '%'.$like['name'].'%')
                ->where('remark', 'like', '%'.$like['remark'].'%')
                ->page($page,$page_size)->select();
            $total = bs::total('classroom');
            $this->apiReturn(200,'',$data,$total);
        }
        if(Request::instance()->method()== 'POST'){
            $value = I('name|remark');
            foreach ($value as $k=>$v){
                if(!$v){
                    $this->apiReturn(303,'lack '.$k);
                }
            }
            $value['create_time'] = time();
            $value['update_time'] = time();

            $id = bs::add('classroom',$value);
            $this->apiReturn(200,'',['id'=>$id]);
        }
        if(Request::instance()->method()== 'PUT'){
            $value = I('id|name|remark');
            foreach ($value as $k=>$v){
                if(!$v){
                    $this->apiReturn(303,'lack '.$k);
                }
            }
            $value['update_time'] = time();
            $id = bs::edit('classroom',$value['id'],$value);
            $this->apiReturn(200,'',['id'=>$id]);
        }
        if(Request::instance()->method()== 'DELETE'){
            $id = I('id');
            if(!$id){
                $this->apiReturn(303,'lack id');
            }
            $return_key = bs::del('classroom',$id);
//            if($return_key){
//                $data = bs::allSelect('classroom','id,name,remark','');
//            }

            $total = bs::total('classroom');
            $this->apiReturn(200,'',$return_key,$total);
        }
    }

}